package bsw.wode.id;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class UserID {
	private static final ArrayList<String> idLibrary = new ArrayList<String>();
	private static final HashMap<String, Long> idTime = new HashMap<String, Long>();
	
	static
	{
		String[] ids = 
		{
			"10000",
			"10001",
			"10002",
			"10003",
		};
		addIDs(ids);
	}
	
	public static String getNewID()
	{
		if(idLibrary.size() <1)
		{
			return null;
		}
		
		String newID = (String)idLibrary.get(idLibrary.size()-1);
		long currentTime = System.currentTimeMillis();
		Long oldTime = idTime.get(newID);
		if( oldTime == null || (currentTime - oldTime > 5000) )
		{
			idTime.put(newID, currentTime);
			return newID;
		}
		else 
		{
			return getNewID();
		}
	}
	
	public static void addIDs(String[] ids)
	{
		for(String id:ids)
		{
			idLibrary.add(id);
		}
	}
	
	public static void removeID(String id)
	{
		idLibrary.remove(id);
		idTime.remove(id);
	}
}
